package com.yinhai.asynctask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yinhai.constant.Contact;
import com.yinhai.dao.ContactGroupDaoImpl;
import com.yinhai.dao.ContactUserDaoImpl;
import com.yinhai.dao.OnlineContactUserDaoImpl;
import com.yinhai.model.ContactGroup;
import com.yinhai.model.ContactsUser;
import com.yinhai.model.OnlineContactDetail;
import com.yinhai.util.PhoneSearchFunction;
import com.yinhai.util.PinYinUtil;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class OnlineContactTaskForLogin  extends AsyncTaskForHttpProcessNoDialog{
	String retStr = null;
	ContactUserDaoImpl contactUserDao;
	OnlineContactUserDaoImpl onlineContactUserDetail;
	public OnlineContactTaskForLogin(Context context) {
		super(context,false);
		contactUserDao = new ContactUserDaoImpl(context);
		onlineContactUserDetail = new OnlineContactUserDaoImpl(context);
	}
	
	public OnlineContactTaskForLogin(Context context, boolean autoForword) {
		super(context, autoForword);
		contactUserDao = new ContactUserDaoImpl(context);
	}

	public OnlineContactTaskForLogin(Context context, int timeoutRequestCount,
			boolean autoForword) {
		super(context, timeoutRequestCount, autoForword);
		contactUserDao = new ContactUserDaoImpl(context);
	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String)result.get(AsyncTaskContext.RESPONSE_CONTENT);
		try {
			JSONObject jsonObject = new JSONObject(retStr);
			JSONArray ja = (JSONArray)((JSONObject)jsonObject.get("obj")).get("data");
			JSONObject jo = null;
			ContactsUser contactUser = null;
			OnlineContactDetail detail = null;
			ContactsUser cuser=null;
			List<ContactsUser> list = new ArrayList<ContactsUser>();
			List<OnlineContactDetail> listdetail = new ArrayList<OnlineContactDetail>();
			for(int i = 0; i < ja.length(); i++) {
				jo = ja.getJSONObject(i);
				contactUser = new ContactsUser();
				detail = new OnlineContactDetail();
				contactUser.setId(Integer.parseInt(jo.get("recid").toString().trim()));
				contactUser.setName(jo.getString("uname"));
				contactUser.setTelPhone(jo.getString("umobile"));
				contactUser.setType(Contact.CONTACT_TYPE_ONLINE);
				contactUser.setGroupId(jo.getString("ctgroupid"));
				contactUser.setGroupName(jo.getString("ctgroupname"));
				contactUser.setFirstpy(PinYinUtil.getFirstSpell(jo.getString("uname")));
				contactUser.setFullpy(PinYinUtil.getFullSpell(jo.getString("uname")));
				contactUser.setNameSimple(PinYinUtil.getString2Num(PinYinUtil.getFirstSpell(jo.getString("uname"))));
				contactUser.setUserid(Integer.parseInt(jo.get("recid").toString().trim())+"");
				
				contactUser.setFullpynum(PinYinUtil.getString2Num(PinYinUtil.getFullSpell(jo.getString("uname"))));
				contactUser.setFullpysep(PinYinUtil.getFullSpellSep(jo.getString("uname")));
				contactUser.setFullpynumsep(PinYinUtil.getString2Num(PinYinUtil.getFullSpellSep(jo.getString("uname"))));
				   contactUser.setIsshowtel(jo.getString("isshowtel"));
	                contactUser.setImUserName(jo.getString("im_user_name"));
				list.add(contactUser);
				
//				long k = contactUserDao.insert(contactUser);
				//detail.setId(Integer.parseInt(jo.get("recid").toString().trim()));
				detail.setUserId(Integer.parseInt(jo.get("recid").toString().trim())+"");
				detail.setName(jo.getString("uname"));
				detail.setCompany(jo.getString("uorg"));
				detail.setDuty(jo.getString("ujob"));
				detail.setTelPhone(jo.getString("umobile"));
				detail.setShortPhone(jo.getString("ushorttel"));
				detail.setMobile(jo.getString("utel"));
				detail.setEmail(jo.getString("uemail"));
			     detail.setIsshowtel(jo.getString("isshowtel"));
				listdetail.add(detail);
				
				if((i%100==0 && i !=0)||(i == (ja.length()-1))){
					SQLiteDatabase db = this.context.openOrCreateDatabase("im.db", Context.MODE_PRIVATE, null);  
			        db.beginTransaction();  
			        for (int j = 0; j < list.size(); j++) {  
			            cuser = list.get(j);
                        db.execSQL("INSERT INTO contact_user(name, tel_phone, type,group_id,group_name,userid,short_num,first_py,name_simple,full_py,full_py_num,full_py_sep,full_py_num_sep,usort,type,isshowtel,im_user_name)"
                            + " values(?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                            new Object[] {cuser.getName(), cuser.getTelPhone(), cuser.getType(), cuser.getGroupId(),
                                cuser.getGroupName(), cuser.getUserid(), cuser.getShortNum(), cuser.getFirstpy(),
                                cuser.getNameSimple(), cuser.getFullpy(), cuser.getFullpynum(), cuser.getFullpysep(),
                                cuser.getFullpynumsep(), cuser.getUsort(), Contact.CONTACT_TYPE_ONLINE,
                                cuser.getIsshowtel(), cuser.getImUserName()});
			        }  
			        db.setTransactionSuccessful();  
			        db.endTransaction(); 
			        db.close();
			        list.clear();
			        
			        SQLiteDatabase db1 = this.context.openOrCreateDatabase("im.db", Context.MODE_PRIVATE, null);
			        db1.beginTransaction();  
			        for (int k = 0; k < listdetail.size(); k++) {  
			        	detail= listdetail.get(k);
			        	 db1.execSQL("INSERT INTO online_contact_detail(userid, name,company,duty,telphone,shortphone,mobile,email)" +
			        	 		" values(?,?,?,?,?,?,?,?)" ,  
			                     new Object[]{detail.getUserId(),detail.getName(),detail.getCompany(),detail.getDuty(),
			        			 detail.getTelPhone(),detail.getShortPhone(),detail.getMobile(),detail.getEmail() });
			        }  
			        db1.setTransactionSuccessful();  
			        db1.endTransaction(); 
			        
			        
			     //  if(db !=null){
			    	   db1.close();
			    	   listdetail.clear();
				}
				
//				onlineContactUserDetail.insert(detail);
			}
			
	      // }
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
